BinaryTree NewTreeNode(TElemSet data) {
    /* 创建数据为data的单根树 */
    BinaryTree node_ptr;

    node_ptr = (BinaryTree)malloc(sizeof(struct BinaryTreeNode));
    node_ptr->data = data;
    node_ptr->first_child = NULL;
    node_ptr->next_sibling = NULL;
    return node_ptr;
}

BinaryTree ConvertTree(Tree tree, int n) {
    BinaryTree root, *nodes;
    Position parent;
    int i;

    if (n == 0) {
        root = NULL; /* 空树 */
    } else {
        /* 创建n个指向树结点的指针 */
        nodes = (BinaryTree *)malloc(sizeof(BinaryTree) * n);
        for (i = 0; i < n; i++) { /* 创建n个单根树 */
            nodes[i] = NewTreeNode(tree[i].data);
        }
        for (i = n - 1; i >= 0; i--) { /* 按编号递减顺序，合并n个单根树 */
            parent = tree[i].parent; /* 记录结点i的父结点 */
            if (parent == NIL) { /* 结点i是根结点 */
                root = nodes[i];
            } else { /* 将结点i插入父结点的子结点链表的表头 */
                nodes[i]->next_sibling = nodes[parent]->first_child;
                nodes[parent]->first_child = nodes[i];
            }
        }
    }
    free(nodes);
    return root;
}